home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / libsrc / xdllist.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  5.4 KB  |  328 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _G_NO_TEMPLATES
  20. #ifdef __GNUG__
  21. //#pragma implementation
  22. #endif
  23. #include <limits.h>
  24. #include <stream.h>
  25. #include <builtin.h>
  26. #include <xdllist.h>
  27.  
  28. void BaseDLList::error(const char* msg)
  29. {
  30.   (*lib_error_handler)("DLList", msg);
  31. }
  32.  
  33. size_t BaseDLList::length()
  34. {
  35.   size_t l = 0;
  36.   BaseDLNode* t = h;
  37.   if (t != 0) do { ++l; t = t->fd; } while (t != h);
  38.   return l;
  39. }
  40.  
  41. // Note:  This is an internal method.  It does *not* free old contents!
  42.  
  43. void BaseDLList::copy(const BaseDLList& a)
  44. {
  45.   if (a.h == 0)
  46.     h = 0;
  47.   else
  48.   {
  49.     BaseDLNode* p = a.h;
  50.     BaseDLNode* t = copy_node(p->item());
  51.     h = t;
  52.     p = p->fd;
  53.     while (p != a.h)
  54.     {
  55.       BaseDLNode* n = copy_node(p->item());
  56.       t->fd = n;
  57.       n->bk = t;
  58.       t = n;
  59.       p = p->fd;
  60.     }
  61.     t->fd = h;
  62.     h->bk = t;
  63.     return;
  64.   }
  65. }
  66.  
  67. void BaseDLList::clear()
  68. {
  69.   if (h == 0)
  70.     return;
  71.  
  72.   BaseDLNode* p = h->fd;
  73.   h->fd = 0;
  74.   h = 0;
  75.  
  76.   while (p != 0)
  77.   {
  78.     BaseDLNode* nxt = p->fd;
  79.     delete_node(p);
  80.     p = nxt;
  81.   }
  82. }
  83.  
  84. BaseDLList& BaseDLList::operator = (const BaseDLList& a)
  85. {
  86.   if (h != a.h)
  87.   {
  88.     clear();
  89.     copy(a);
  90.   }
  91.   return *this;
  92. }
  93.  
  94.  
  95. Pix BaseDLList::prepend(void *datum)
  96. {
  97.   BaseDLNode* t = copy_node(datum);
  98.   if (h == 0)
  99.     t->fd = t->bk = h = t;
  100.   else
  101.   {
  102.     t->fd = h;
  103.     t->bk = h->bk;
  104.     h->bk->fd = t;
  105.     h->bk = t;
  106.     h = t;
  107.   }
  108.   return Pix(t);
  109. }
  110.  
  111. Pix BaseDLList::append(void *datum)
  112. {
  113.   BaseDLNode* t = copy_node(datum);
  114.   if (h == 0)
  115.     t->fd = t->bk = h = t;
  116.   else
  117.   {
  118.     t->bk = h->bk;
  119.     t->bk->fd = t;
  120.     t->fd = h;
  121.     h->bk = t;
  122.   }
  123.   return Pix(t);
  124. }
  125.  
  126. Pix BaseDLList::ins_after(Pix p, void *datum)
  127. {
  128.   if (p == 0) return prepend(datum);
  129.   BaseDLNode* u = (BaseDLNode*) p;
  130.   BaseDLNode* t = copy_node(datum);
  131.   t->bk = u;
  132.   t->fd = u->fd;
  133.   u->fd->bk = t;
  134.   u->fd = t;
  135.   return Pix(t);
  136. }
  137.  
  138. Pix BaseDLList::ins_before(Pix p, void *datum)
  139. {
  140.   if (p == 0) error("null Pix");
  141.   BaseDLNode* u = (BaseDLNode*) p;
  142.   BaseDLNode* t = copy_node(datum);
  143.   t->bk = u->bk;
  144.   t->fd = u;
  145.   u->bk->fd = t;
  146.   u->bk = t;
  147.   if (u == h) h = t;
  148.   return Pix(t);
  149. }
  150.  
  151. void BaseDLList::join(BaseDLList& b)
  152. {
  153.   BaseDLNode* t = b.h;
  154.   b.h = 0;
  155.   if (h == 0)
  156.     h = t;
  157.   else if (t != 0)
  158.   {
  159.     BaseDLNode* l = t->bk;
  160.     h->bk->fd = t;
  161.     t->bk = h->bk;
  162.     h->bk = l;
  163.     l->fd = h;
  164.   }
  165. }
  166.  
  167. int BaseDLList::owns(Pix p)
  168. {
  169.   BaseDLNode* t = h;
  170.   if (t != 0 && p != 0)
  171.   {
  172.     do
  173.     {
  174.       if (Pix(t) == p) return 1;
  175.       t = t->fd;
  176.     } while (t != h);
  177.   }
  178.   return 0;
  179. }
  180.  
  181. void BaseDLList::del(Pix& p, int dir)
  182. {
  183.   if (p == 0) error("null Pix");
  184.   BaseDLNode* t = (BaseDLNode*) p;
  185.   if (t->fd == t)
  186.   {
  187.     h = 0;
  188.     p = 0;
  189.   }
  190.   else
  191.   {
  192.     if (dir < 0)
  193.     {
  194.       if (t == h)
  195.         p = 0;
  196.       else
  197.         p = Pix(t->bk);
  198.     }
  199.     else
  200.     {
  201.       if (t == h->bk)
  202.         p = 0;
  203.       else
  204.         p = Pix(t->fd);
  205.     }
  206.     t->bk->fd = t->fd;
  207.     t->fd->bk = t->bk;
  208.     if (t == h) h = t->fd;
  209.   }
  210.   delete t;
  211. }
  212.  
  213. void BaseDLList::del_after(Pix& p)
  214. {
  215.   if (p == 0)
  216.   {
  217.     del_front();
  218.     return;
  219.   }
  220.  
  221.   BaseDLNode* b = (BaseDLNode*) p;
  222.   BaseDLNode* t = b->fd;
  223.  
  224.   if (b == t)
  225.   {
  226.     h = 0;
  227.     p = 0;
  228.   }
  229.   else
  230.   {
  231.     t->bk->fd = t->fd;
  232.     t->fd->bk = t->bk;
  233.     if (t == h) h = t->fd;
  234.   }
  235.   delete_node(t);
  236. }
  237.  
  238. void BaseDLList::remove_front(void *dst)
  239. {
  240.   if (h == 0)
  241.     error("remove_front of empty list");
  242.   else {
  243.       BaseDLNode* t = h;
  244.       copy_item(dst, t->item());
  245.       if (h->fd == h)
  246.       h = 0;
  247.       else
  248.       {
  249.           h->fd->bk = h->bk;
  250.           h->bk->fd = h->fd;
  251.           h = h->fd;
  252.       }
  253.       delete_node(t);
  254.   }
  255. }
  256.  
  257. void BaseDLList::del_front()
  258. {
  259.   if (h == 0)
  260.     error("del_front of empty list");
  261.   BaseDLNode* t = h;
  262.   if (h->fd == h)
  263.     h = 0;
  264.   else
  265.   {
  266.     h->fd->bk = h->bk;
  267.     h->bk->fd = h->fd;
  268.     h = h->fd;
  269.   }
  270.   delete_node(t);
  271. }
  272.  
  273. void BaseDLList::remove_rear(void *dst)
  274. {
  275.   if (h == 0)
  276.     error("remove_rear of empty list");
  277.   else
  278.     {
  279.       BaseDLNode* t = h->bk;
  280.       copy_item(dst, t->item());
  281.       if (h->fd == h)
  282.     h = 0;
  283.       else
  284.     {
  285.       t->fd->bk = t->bk;
  286.       t->bk->fd = t->fd;
  287.         }
  288.       delete_node(t);
  289.     }
  290. }
  291.  
  292. void BaseDLList::del_rear()
  293. {
  294.   if (h == 0)
  295.     error("del_rear of empty list");
  296.   BaseDLNode* t = h->bk;
  297.   if (h->fd == h)
  298.     h = 0;
  299.   else
  300.   {
  301.     t->fd->bk = t->bk;
  302.     t->bk->fd = t->fd;
  303.   }
  304.   delete_node(t);
  305. }
  306.  
  307.  
  308. int BaseDLList::OK()
  309. {
  310.   int v = 1;
  311.   if (h != 0)
  312.   {
  313.     BaseDLNode* t = h;
  314.     long count = LONG_MAX;      // Lots of chances to find h!
  315.     do
  316.     {
  317.       count--;
  318.       v &= t->bk->fd == t;
  319.       v &= t->fd->bk == t;
  320.       t = t->fd;
  321.     } while (v && count > 0 && t != h);
  322.     v &= count > 0;
  323.   }
  324.   if (!v) error("invariant failure");
  325.   return v;
  326. }
  327. #endif
  328.